#! /usr/bin/python3
# -*- coding: utf-8 -*-
# __author__ = "Fengxuewei"
# Date: 2020/6/30

#1、如何让字典保持有序？
#================================================================================================
# 实际案例： 某编程竞赛系统，对参赛选手编程解题进行计时，选手完成题目后，把该选手解题用时记录到字典中，以便赛后按选手名查询成绩，
#（答题用时越短，成绩越优）
# {'lilei':(2,43), 'HanMeimei':(5,52), 'Jim':(1,39)...}
# 比赛结束后，需按排名顺序依次打印选手成绩，如何实现？

# 解决方案：
# 使用collections.OrderedDict
# 以OrderedDict替代内置字典Dict，依次将选手成绩存入OrderedDict。


# 传统的字典Dict是不按进入字典的先后顺序来排序的
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

d = {}

d['Jim'] = (1, 35)
d['Leo'] = (2, 37)
d['Bob'] = (3, 40)

for i in d:
    print(i)



# 使用collections.OrderedDict
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

from collections import OrderedDict
d = OrderedDict()
d['Jim'] = (1, 35)
d['Leo'] = (2, 37)
d['Bob'] = (3, 40)
for i in d:
    print(i)

# 模拟实现
#+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
from time import time
from random import randint
from collections import OrderedDict

d = OrderedDict()
players = list('ABCDEFGH')
num_player = len(players)
start = time()
for i in range(num_player):
    input()
    p = players.pop(randint(0, num_player - 1 - i))
    end = time()
    print(i + 1, p, end - start)
    d[p] = (i + 1, end - start)

print(" ")
print('-' * 20)
for k in d:
    print(k, d[k])